package com.lhy.framework.context.support;

import com.lhy.framework.beans.BeansException;
import com.lhy.framework.beans.factory.factory.BeanPostProcessor;
import com.lhy.framework.context.ApplicationContext;
import com.lhy.framework.context.ApplicationContextAware;

/**
 * @ClassName ApplicationContextAwareProcessor
 * @Description 包装处理器
 * @Author lihaiyang
 * @Date 2022/9/12 21:51
 * @Version 1.0
 */
public class ApplicationContextAwareProcessor implements BeanPostProcessor {

    private final ApplicationContext applicationContext;

    public ApplicationContextAwareProcessor(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }

    @Override
    public Object postProcessBeforeInitialization(String beanName, Object bean) throws BeansException {
        if (bean instanceof ApplicationContextAware) {
            ((ApplicationContextAware) bean).setApplicationContext(applicationContext);
        }
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(String beanName, Object bean) throws BeansException {
        return bean;
    }
}
